package com.example.qimo.Diray;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.qimo.R;
import com.example.qimo.utils.MyDiaryDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Diray extends AppCompatActivity {


    MyDiaryDatabase mdb;
    ListView listDb;
    private List data = new ArrayList();
    private int nid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_deal);
        listDb = findViewById(R.id.list);
        mdb = new MyDiaryDatabase(this);
        mdb.openDb();
        mdb.onCreateTable();
        init();
        //设置列表项的点击事件
        listDb.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //页面操作跳转时 附加数据
                HashMap map = (HashMap) data.get(i);
                nid = (int)map.get("id");
                Intent intent = new Intent(Diray.this,DiaryDetail.class);
                Bundle bb = new Bundle();
                bb.putInt("ID",nid);
                bb.putInt("flag",1);//1表示修改  0表示添加
                intent.putExtras(bb);
                startActivityForResult(intent,0);
            }
        });
    }

    private void init() {
        Cursor cursor = mdb.GetAllData();
        data.clear();
        if (cursor != null) {
            int num = cursor.getCount();
            if (num == 0) {
                Toast.makeText(this, "没有数据,请先添加数据", Toast.LENGTH_LONG).show();
                return;
            }
            while (cursor.moveToNext()) {
                int id = cursor.getInt(0);
                String title = cursor.getString(1);
                String body = cursor.getString(2);
                HashMap map = new HashMap();
                map.put("id", id);
                map.put("title", title);
                map.put("body", body);
                data.add(map);
            }
            cursor.close();
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.diary_row, new String[]{"title", "body"}, new int[]{R.id.text1,R.id.text2});
            listDb.setAdapter(simpleAdapter);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, 1, 1, "添加一篇新日记");
        return true;
    }
//选项菜单事件

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == 1) {
            Intent intent = new Intent(this, DiaryDetail.class);
            //添加flag标识,添加的操纵，赋值
            Bundle bb = new Bundle();
            bb.putInt("flag",0);
            intent.putExtras(bb);
            startActivityForResult(intent,0);
        } else {
            return super.onOptionsItemSelected(item);
        }
        return true;
    }
}